In [1]:
from collections import OrderedDict
import sys
import base_node_rpc.node as bn
sys.path.insert(0, '../..')
import magnet_control as mc
from nadamq.NadaMq import cPacket, cPacketParser, PACKET_TYPES, PACKET_NAME_BY_TYPE
from nanopb_helpers import dump_message
from serial import Serial
import pandas as pd
import numpy as np
In [14]:
import time
# serial_device = Serial('/dev/ttyUSB0', baudrate=115200)
serial_device = Serial('/dev/ttyUSB2', baudrate=115200)
# serial_device = Serial('/dev/ttyACM0', baudrate=115200)
proxy = mc.Proxy(serial_device)
retry_count = 5
for i in range(retry_count):
try:
properties = proxy.properties()
except IOError:
if i >= retry_count - 1:
raise
print properties
In [17]:
print '%(serial_number)03d' % {'serial_number': 3}
In [16]:
properties.to_dict()
Out[16]:
In [15]:
properties['name']
Out[15]:
In [9]:
import datetime as dt
In [11]:
dt.timedeltakkkkkkkkkkkkkkkkkk
Out[11]:
In [110]:
config = mc.Config.FromString(proxy.serialize_config().tostring())
print dump_message(config)
In [15]:
proxy.servo_attach(9)
In [16]:
proxy.servo_write(110)
In [17]:
proxy.servo_write(75)
In [18]:
from IPython.html.widgets import interactive
In [24]:
def set_state(state=False):
if state:
proxy.servo_write(75)
else:
proxy.servo_write(103)
interactive(set_state)
In [12]:
serial_device.baudrate
Out[12]:
In [ ]:
serial_device.close()
In [22]:
Out[22]:
In [5]:
# Set several motor attributes at once using `update_state` method.
proxy.update_state(motor_delay_us=int(1e3), motor_enabled=True, motor_pulse_us=20)
Out[5]:
In [6]:
# Reset current motor position as position 0.
proxy.motor_set_home()
In [7]:
# Query current motor position.
proxy.position()
Out[7]:
In [8]:
# Set motor speed (steps per second).
proxy.motor_set_speed(1000)
In [9]:
# Set target position to half turn (assuming 200 steps/revolution motor).
proxy.set_target_position(100)
In [10]:
# Set target position origin.
proxy.set_target_position(0)
In [11]:
# Move steps relative to current position.
# Positive values are clockwise, negative counter-clockwise.
proxy.move(-1)
In [12]:
# If the motor is enabled, start moving the motor at 20 steps per second.
proxy.motor_start(-400)
In [13]:
# Stop the motor (but do not disable it).
proxy.motor_stop()
In [14]:
from IPython.html.widgets import interactive
def update_motor(position=0, speed=1000, enabled=False, microstep_mode='full'):
if enabled:
# Change microstep setting (STEP_SETTINGS.(full,half,quarter,eighth,sixteeth)
proxy.set_MS(*getattr(STEP_SETTINGS, microstep_mode).values)
# Set motor speed (steps per second).
proxy.motor_set_speed(speed)
proxy.update_state(motor_enabled=True)
proxy.set_target_position(position)
else:
proxy.update_state(motor_enabled=False)
interactive(update_motor, position=(-400, 400), speed=(200, 1600),
microstep_mode=('full', 'half', 'quarter', 'eighth', 'sixteenth'))